export default class Tool {
	// 节流
	static throttle (fn, delay) {
		let timer = null;
		let starTime = new Date();
		return () => {
			let curTime = new Date();
			clearInterval(timer);
			// 规定的时间触发，不然用户一直操作就一直不会触发
			if (curTime - starTime >= delay) {
				starTime = curTime;
				fn();
			} else {
				timer = setTimeout(fn, delay);
			}
		};
	}

	// 防抖
	static debounce (fn, delay) {
		let timer = null;
		return () => {
			clearInterval(timer);
			timer = setTimeout(fn, delay);
		};
	}

	// 路由转map对象
	static routesToMap (asyncRoutes, routesMap = {}) {
		asyncRoutes.forEach(item => {
			let temp = { ...item };
			if (item.children && item.children.length > 0) {
				delete temp.children;
				routesMap[item.path] = temp;
				this.routesToMap(item.children, routesMap);
			} else {
				routesMap[item.path] = temp;
			}
		});
		return routesMap;
	}

	// 映射路由表
	static mappingRoutes (tree, routesMap, parentPath) {
		tree.forEach(item => {
			// 优先取接口字段
			const temp = routesMap[item.path];
			temp.meta.title = item.name || temp.meta.title;
			temp.meta.icon = item.icon || temp.meta.icon;
			item.meta = temp.meta;
			item.component = temp.component;
			parentPath && (item.path = parentPath + '/' + item.path);
			if (item.children && item.children.length > 0) {
				this.mappingRoutes(item.children, routesMap, item.path);
			}
		});
		return tree;
	}

	// 路由过滤
	static filterAsyncRoutes (routes, roles) {
		const res = [];
		routes.forEach(route => {
			const tmp = { ...route };
			if (this.hasPermission(roles, tmp)) {
				if (tmp.children) {
					tmp.children = this.filterAsyncRoutes(tmp.children, roles);
				}
				res.push(tmp);
			}
		});

		return res;
	}

	// 权限校验
	static hasPermission (roles, route) {
		if (route.meta && route.meta.roles && route.meta.roles.length > 0) {
			return roles.some(role => route.meta.roles.includes(role));
		} else {
			return true;
		}
	}

	// 后缀名
	static extension (str) {
		return str.substring(str.lastIndexOf('.') + 1);
	}

	// 提取金额最多2个小数
	static getAmount (digitalStr) {
		return digitalStr.replace(/^\D*([1-9]\d*\.?\d{1,2})?.*$/, '$1');
	}

	// 获取上一个月时间
	static getPreMonth (date) {
		var d = new Date(date);
		d.setMonth(d.getMonth() - 1);
		return d;
	}

	// 流文件下载
	static fileDdownload (data) {
		let blob = new Blob([data], { type: 'application/vnd.ms-excel;charset=UTF-8' });
		let url = window.URL.createObjectURL(blob);
		let aLink = document.createElement('a');
		aLink.style.display = 'none';
		aLink.href = url;
		aLink.setAttribute('download', 'filename');
		document.body.appendChild(aLink);
		aLink.click();
		document.body.removeChild(aLink);
		window.URL.revokeObjectURL(url);
	}
	// form文件下载
	static fileDdownloada (url) {
		var dlform = document.createElement('form');
		dlform.style = 'display:none;';
		dlform.method = 'post';
		dlform.action = url;
		dlform.target = 'callBackTarget';
		var hdnFilePath = document.createElement('input');
		hdnFilePath.type = 'hidden';
		hdnFilePath.name = 'filePath';
		hdnFilePath.value = 'filename';
		dlform.appendChild(hdnFilePath);
		document.body.appendChild(dlform);
		dlform.submit();
		document.body.removeChild(dlform);
	}

}
